package com.work.keep.refresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.work.keep.R;

/**
 * Created by dllo on 16/1/18.
 */
public class LoadLayoutManager extends StateLayoutManager {
    private RelativeLayout myFootContainer;
    private ProgressBar footProgressbar;
    private TextView footStatusAlert;

    public LoadLayoutManager(Context context) {
        this(context, null);
    }

    public LoadLayoutManager(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadLayoutManager(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        footProgressbar = (ProgressBar) findViewById(R.id.pull_to_load_progressbar);
        footStatusAlert = (TextView) findViewById(R.id.footerStatusAlert);
        //Todo 为什么让它先进入reset状态
        setLoadState(LoadState.RESET);
    }

    @Override
    protected View createContainView(Context context, AttributeSet attrs) {
        myFootContainer = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.layout_footer_one, null);
        return myFootContainer;
    }

    @Override
    protected void onStateChanged(LoadState curState, LoadState preState) {
        //设置常态状态
        footProgressbar.setVisibility(GONE);
//        footStatusAlert.setVisibility(INVISIBLE);
        //
        super.onStateChanged(curState, preState);
    }

    @Override
    protected void onNoMoreData() {
        footStatusAlert.setText("没有更多数据可以加载");
    }

    @Override
    protected void onRefreshing() {
        footProgressbar.setVisibility(VISIBLE);
        footStatusAlert.setText("正在刷新");
    }

    //进入pull动作
    @Override
    protected void onPullToRefresh() {
        if (LoadState.RELEASE_TO_REFRESH == getPreState()) {

        }
        footStatusAlert.setText("查看更多");
    }

    @Override
    protected void onReadyForReleaseToRefresh() {
        footStatusAlert.setText("松开开始加载");
    }

    @Override
    protected void onReset() {
        //这里放最初出现在界面中的话
        footStatusAlert.setText("查看更多");
    }

    @Override
    public int getContentSize() {
        if (null == myFootContainer) {
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) myFootContainer.getLayoutParams();
            return params.height;
        }
        return (int) (getResources().getDisplayMetrics().density * 40);
    }
}
